/*
	Copyright 2009 Benjamin Vedder	vedder87@gmail.com
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.google.code.retrobox.editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;

import com.google.code.retrobox.editor.commons.dialog.AboutDialog;
import com.google.code.retrobox.editor.dataCollections.MapData;
import com.google.code.retrobox.editor.dataCollections.TileSet;
import com.google.code.retrobox.editor.maps.MapEditor;
import com.google.code.retrobox.editor.maps.MapIO;
import com.google.code.retrobox.editor.sprites.SpriteEditor;
import com.google.code.retrobox.editor.sprites.SpriteIO;
import com.google.code.retrobox.editor.tiles.TileEditor;
import com.google.code.retrobox.editor.tiles.TilesIO;
import com.google.code.retrobox.editor.tracker.TrackerEditor;
import com.google.code.retrobox.editor.tracker.TrackerIO;

public class MainWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String versionNumber = "0.006";

	// Self reference
	private MainWindow self = this;

	// Stuff
	JFileChooser fc = new JFileChooser();

	private MapEditor editor = new MapEditor(TestSet.TILESET, TestSet.COLLDATA,
			TestSet.MAP1, 60, 30, this);
	private TileEditor tEditor = new TileEditor(editor.tileset, this);
	private SpriteEditor sEditor = new SpriteEditor(this);
	private TrackerEditor trEditor = new TrackerEditor(this);
	private SpriteIO sIO = new SpriteIO();
	private MapIO mapIO = new MapIO();
	private TilesIO tilesIO = new TilesIO();
	private TrackerIO trackerIO = new TrackerIO();
	
	// Components
	JTabbedPane tp = new JTabbedPane();

	// Menubar
	private JMenuBar menuBar = new JMenuBar();

	private JMenu fileMenu = new JMenu("File");

	private JMenuItem newMapItem = new JMenuItem("New map");
	private JMenuItem newTilesetItem = new JMenuItem("New tileset");
	private JMenuItem newSpriteItem = new JMenuItem("New sprite");

	private JMenuItem exportMapItem = new JMenuItem("Export map to header");
	private JMenuItem exportTileSetItem = new JMenuItem(
			"Export tileset to header");
	private JMenuItem exportSpriteItem = new JMenuItem(
			"Export sprites to header");
	private JMenuItem exportSpriteAsGameduinoItem = new JMenuItem(
	"Export sprites to gameduino");
	private JMenuItem exportMusicItem = new JMenuItem(
	"Export music to header");
	
	private JMenuItem saveMapItem = new JMenuItem("Save map");
	private JMenuItem saveTileSetItem = new JMenuItem("Save tileset");
	private JMenuItem saveSpriteSetItem = new JMenuItem("Save spriteset");
	private JMenuItem saveMusicItem = new JMenuItem("Save music");
	private JMenuItem loadMapItem = new JMenuItem("Load map");
	private JMenuItem loadTileSetItem = new JMenuItem("Load tileset");
	private JMenuItem loadSpriteSetItem = new JMenuItem("Load spriteset");
	private JMenuItem loadMusicItem = new JMenuItem("Load music");
	private JMenuItem importMapItem = new JMenuItem("Import map from header");
	private JMenuItem importTileSetItem = new JMenuItem(
			"Import tileset from header");
	private JMenuItem importMusicAsRTTTLItem = new JMenuItem(
			"Import music from RTTTL");
	private JMenuItem importSpriteItem = new JMenuItem(
	"Import sprites from header");
	
	private JMenu setupMenu = new JMenu("Setup");

	private JMenuItem setupMidiItem = new JMenuItem("Config Midi keyboard");
	
	private JMenu helpMenu = new JMenu("Help");

	private JMenuItem aboutItem = new JMenuItem("About");

	private ActionListener menuListener = new ActionListener() {

		public void actionPerformed(ActionEvent e) {

			// File menu
			if (e.getSource() == newMapItem) {
				newMap();
			}

			else if (e.getSource() == newTilesetItem) {
				newTileset();
			}

			else if (e.getSource() == newSpriteItem) {
				// TODO
			}

			else if (e.getSource() == exportMapItem) {
				exportMap();
			}

			else if (e.getSource() == exportTileSetItem) {
				exportTileset();
			}

			else if (e.getSource() == exportSpriteItem) {
				exportSprite();
			}
			else if (e.getSource() == exportSpriteAsGameduinoItem) {
				exportSpriteAsGameduino();
			}
			else if (e.getSource() == exportMusicItem) {
				exportMusic();
			}

			else if (e.getSource() == saveMapItem) {
				saveMap();
			}

			else if (e.getSource() == saveTileSetItem) {
				saveTileset();
			}

			else if (e.getSource() == saveSpriteSetItem) {
				saveSpriteSet();
			}
			else if (e.getSource() == saveMusicItem) {
				saveMusic();
			}
			else if (e.getSource() == loadMapItem) {
				loadMap();
			}			
			else if (e.getSource() == loadTileSetItem) {
				loadTileSet();
			}			
			else if (e.getSource() == loadSpriteSetItem) {
				loadSpriteSet();
			}
			else if (e.getSource() == loadSpriteSetItem) {
				loadMusic();
			}
			else if (e.getSource() == importMapItem) {
				importMap();
			}

			else if (e.getSource() == importTileSetItem) {
				importTileset();
			}

			else if (e.getSource() == importSpriteItem) {
				importSprites();
			}
			else if (e.getSource() == importMusicAsRTTTLItem) {
				importMusicAsRTTTL();
			}			
			// Setup menu
			else if (e.getSource() == setupMidiItem) {
				setupMidiKeyboard();
			}
			// Help menu
			else if (e.getSource() == aboutItem) {
				new AboutDialog(self);
			}
		}



	};

	public MainWindow() {
		setLayout(new BorderLayout());

		setJMenuBar(menuBar);
		menuBar.add(fileMenu);

		fileMenu.add(newMapItem);
		fileMenu.add(newTilesetItem);
		fileMenu.add(newSpriteItem);
		fileMenu.addSeparator();
		fileMenu.add(exportMapItem);
		fileMenu.add(exportTileSetItem);
		fileMenu.add(exportSpriteItem);
		fileMenu.add(exportSpriteAsGameduinoItem);		
		fileMenu.add(exportMusicItem);		
		fileMenu.addSeparator();
		fileMenu.add(saveMapItem);
		fileMenu.add(saveTileSetItem);
		fileMenu.add(saveSpriteSetItem);
		fileMenu.add(saveMusicItem);
		fileMenu.addSeparator();
		fileMenu.add(loadMapItem);
		fileMenu.add(loadTileSetItem);
		fileMenu.add(loadSpriteSetItem);
		fileMenu.add(loadMusicItem);		
		fileMenu.addSeparator();
		fileMenu.add(importMapItem);
		fileMenu.add(importTileSetItem);
		fileMenu.add(importSpriteItem);
		fileMenu.add(importMusicAsRTTTLItem);
		
		menuBar.add(setupMenu);

		setupMenu.add(setupMidiItem);

		menuBar.add(helpMenu);

		helpMenu.add(aboutItem);

		newMapItem.addActionListener(menuListener);
		newTilesetItem.addActionListener(menuListener);
		newSpriteItem.addActionListener(menuListener);
		exportMapItem.addActionListener(menuListener);
		exportSpriteItem.addActionListener(menuListener);
		exportSpriteAsGameduinoItem.addActionListener(menuListener);
		exportTileSetItem.addActionListener(menuListener);
		exportMusicItem.addActionListener(menuListener);
		saveMapItem.addActionListener(menuListener);
		saveTileSetItem.addActionListener(menuListener);
		saveSpriteSetItem.addActionListener(menuListener);
		saveMusicItem.addActionListener(menuListener);
		loadMapItem.addActionListener(menuListener);
		loadTileSetItem.addActionListener(menuListener);
		loadSpriteSetItem.addActionListener(menuListener);
		loadMusicItem.addActionListener(menuListener);		
		importMapItem.addActionListener(menuListener);
		importTileSetItem.addActionListener(menuListener);
		importSpriteItem.addActionListener(menuListener);
		importMusicAsRTTTLItem.addActionListener(menuListener);
		
		setupMidiItem.addActionListener(menuListener);

		aboutItem.addActionListener(menuListener);

		tp.addTab("Map editor", editor);
		tp.addTab("Tile editor", tEditor);
		tp.addTab("Sprite editor", sEditor);
		tp.addTab("Beep tracker", trEditor);

		sEditor.setSprite(TestSet.MARIO16);

		add(tp, BorderLayout.CENTER);

		setTitle("RetroBox Game Assets Manager");
		setSize(new Dimension(1000, 650));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);

	}

	public void addEmptyTiles(int n) {
		try {
			tp.remove(tEditor);
		} catch (Exception e) {

		}

		TileSet ts = editor.tileset;

		ts.addEmptyTiles(n);

		editor.setTileset(ts);

		tEditor = new TileEditor(ts, this);

		tp.insertTab("Tile editor", null, tEditor, "Tile editor", 1);
		tp.setSelectedIndex(1);

		validate();
	}

	public void removeTiles(int n) {
		try {
			tp.remove(tEditor);
		} catch (Exception e) {

		}

		TileSet ts = editor.tileset;

		ts.removeTiles(n);

		editor.setTileset(ts);

		tEditor = new TileEditor(ts, this);

		tp.insertTab("Tile editor", null, tEditor, "Tile editor", 1);
		tp.setSelectedIndex(1);

		validate();
	}

	public void newMap() {

		String a = JOptionPane.showInputDialog(this,
				"Enter the dimensions, separate them with space.", "New map",
				JOptionPane.OK_CANCEL_OPTION);

		if (a == null) {
			return;
		}

		int w, h;

		try {
			Scanner sc = new Scanner(a);
			w = sc.nextInt();
			h = sc.nextInt();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,
					"Bad input. Please check what you "
							+ "have typed and try again.");
			return;
		}

		try {
			tp.remove(editor);
		} catch (Exception e) {

		}

		MapData md = new MapData(editor.tileset, w, h);

		editor = new MapEditor(md, this);

		tp.insertTab("Map editor", null, editor, null, 0);
		tp.setSelectedIndex(0);

		validate();
	}

	public void newTileset() {

		String a = JOptionPane
				.showInputDialog(
						this,
						"<html>Enter the number of tiles (1 - 256). <BR><font color=red>WARNING: this will destroy the current map!</font color=red></html>",
						"New tileset", JOptionPane.OK_CANCEL_OPTION);

		if (a == null) {
			return;
		}

		int n;

		try {
			Scanner sc = new Scanner(a);
			n = sc.nextInt();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,
					"Bad input. Please check what you "
							+ "have typed and try again.");
			return;
		}

		try {
			tp.remove(tEditor);
		} catch (Exception e) {

		}

		TileSet ts = new TileSet(n);

		editor.setTileset(ts);

		tEditor = new TileEditor(ts, this);

		tp.insertTab("Tile editor", null, tEditor, null, 1);
		tp.setSelectedIndex(0);

		validate();
	}

	public void exportMap() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}
		try {
			mapIO.exporMapToHeader(file, editor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Map header file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error exporting file. <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}

	public void exportTileset() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			tilesIO.exportTileSetAsHeader(file, tEditor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Tileset header file has been written to <br><font color=green>"
									+ file.getCanonicalPath() + "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane
			.showMessageDialog(
					this,
					"<html>"
							+ "Error exporting tileset <br><font color=green>"
							+ file.getAbsolutePath() + "</font color=green></html>");
		}
	}

	public void exportSprite() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			sIO.exportToHeader(file, sEditor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Tileset header file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error exporting Sprite <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}

	public void exportSpriteAsGameduino() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			sIO.exportToGameduinoHeader(file, sEditor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Tileset header file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error exporting Sprite <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}

	public void saveMap() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}
		try {
			mapIO.saveMap(file, editor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Map file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error saving file. <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}

	public void saveTileset() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			tilesIO.saveTileSet(file, tEditor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Tileset file has been written to <br><font color=green>"
									+ file.getCanonicalPath() + "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane
			.showMessageDialog(
					this,
					"<html>"
							+ "Error exporting tileset <br><font color=green>"
							+ file.getAbsolutePath() + "</font color=green></html>");
		}	
	}

	public void saveSpriteSet() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {

			this.sIO.save(file, sEditor);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Sprite header file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");

		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error saving Sprite <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}

	public void loadSpriteSet() {
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			sIO.load(file, sEditor);
			tp.setSelectedIndex(tp.getTabCount() - 1);

			validate();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error loading Sprite <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");

		}

	}
	
	public void loadMap(){
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}
		
		MapData md = null;
		try{
			md = mapIO.loadMap(file, editor);
			tp.remove(editor);
			editor = new MapEditor(md, this);

			tp.insertTab("Map editor", null, editor, null, 0);
			tp.setSelectedIndex(0);

			validate();
		}catch(Exception e){
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error loading tilemap <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
	}
	
	public void loadTileSet() {
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}



		try {
			TileSet ts = tilesIO.loadTileSet(file, tEditor);
			tp.remove(tEditor);
			editor.setTileset(ts);

			tEditor = new TileEditor(ts, this);

			tp.insertTab("Tile editor", null, tEditor, "Tile editor", 1);
			tp.setSelectedIndex(0);

			validate();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error loading tileset <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}



		
	}
	public void importMap() {
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}
		
		MapData md = null;
		try{
			md = mapIO.importMapFromHeader(file, editor);
			tp.remove(editor);
			editor = new MapEditor(md, this);

			tp.insertTab("Map editor", null, editor, null, 0);
			tp.setSelectedIndex(0);

			validate();
		}catch(Exception e){
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error importing tilemap <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}

	}

	public void importTileset() {
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}



		try {
			TileSet ts = tilesIO.importTileset(file, tEditor);
			tp.remove(tEditor);
			editor.setTileset(ts);

			tEditor = new TileEditor(ts, this);

			tp.insertTab("Tile editor", null, tEditor, "Tile editor", 1);
			tp.setSelectedIndex(0);

			validate();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error importing tileset <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}



		
	}

	public void importSprites() {
		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			sIO.importFromHeader(file, sEditor);
			tp.setSelectedIndex(tp.getTabCount() - 1);

			validate();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error importing sprites: <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}

	}
	
	public void setupMidiKeyboard(){
		trEditor.askMidiInPort();
	}
	
	public void importMusicAsRTTTL() {

		int returnVal = fc.showOpenDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (!file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this, "File not found...") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			trackerIO.importFromRTTTL(file, trEditor);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error importing RTTTL file: <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}

		
	}

	public void loadMusic() {
		// TODO Auto-generated method stub
		
	}

	public void saveMusic() {
		// TODO Auto-generated method stub
		
	}

	public void exportMusic() {
		int returnVal = fc.showSaveDialog(this);

		File file;

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		} else {
			return;
		}

		if (file.exists()) {
			if (!(JOptionPane.showConfirmDialog(this,
					"Overwrite existing file?") == JOptionPane.OK_OPTION)) {
				return;
			}
		}

		try {
			trackerIO.exportToHeader(file, trEditor.pattern, trEditor.bpm);
			JOptionPane
					.showMessageDialog(
							this,
							"<html>"
									+ "Music header file has been written to <br><font color=green>"
									+ file.getAbsolutePath()
									+ "</font color=green></html>");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "<html>"
					+ "Error exporting music <br><font color=green>"
					+ e.getLocalizedMessage() + "</font color=green></html>");
		}
		
	}

	public static void main(String[] arg) {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
			// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}

		new MainWindow();
	}
}